import type { AppState, AppConfig } from "./types";

export const actions = {
	switchVersionDialog(this: AppState, payload: boolean): void {
		this.isVersionDialog = payload;
	},

	updateVersion(this: AppState, payload: string): void {
		this.version = payload;
	},
	updateNewVersion(this: AppState, payload: string): void {
		this.newVersion = payload;
	},

	/**
	 * 更新应用配置
	 */
	updateConfig(this: AppState, config: Partial<AppConfig>): void {
		this.config = {
			...this.config,
			...config,
		};
	},

	/**
	 * 切换侧边栏收起状态
	 */
	toggleCollapsed(this: AppState): void {
		this.config.collapsed = !this.config.collapsed;
	},

	/**
	 * 添加缓存页面
	 */
	addCachedView(this: AppState, view: string): void {
		if (this.cachedViews.includes(view)) return;
		this.cachedViews.push(view);
	},

	/**
	 * 移除缓存页面
	 */
	removeCachedView(this: AppState, view: string): void {
		const index = this.cachedViews.indexOf(view);
		if (index > -1) {
			this.cachedViews.splice(index, 1);
		}
	},

	/**
	 * 添加访问页面
	 */
	addVisitedView(this: AppState, view: string): void {
		if (this.visitedViews.includes(view)) return;
		this.visitedViews.push(view);
	},

	/**
	 * 移除访问页面
	 */
	removeVisitedView(this: AppState, view: string): void {
		const index = this.visitedViews.indexOf(view);
		if (index > -1) {
			this.visitedViews.splice(index, 1);
		}
	},

	/**
	 * 清空所有缓存页面
	 */
	clearCachedViews(this: AppState): void {
		this.cachedViews = [];
	},

	/**
	 * 清空所有访问页面
	 */
	clearVisitedViews(this: AppState): void {
		this.visitedViews = [];
	},
};
